#include"Stack.h"

void StackInit(Stack* s)
{
	assert(s);
	s->a = NULL;
	s->capacity = s->size = 0;
}

void StackDestory(Stack* s)
{
	assert(s);
	free(s->a);
	s->a = NULL;
	s->capacity = s->size = 0;
}


bool StackFull(Stack* s)
{
	assert(s);
	return s->capacity == s->size;
}

bool StackEmpty(Stack* s)
{
	assert(s);
	return (s->size == 0);
}

void StackPush(Stack* s, SData x)
{
	assert(s);
	if (StackFull(s))
	{
		int newcapacity = s->capacity == 0 ? 4 : s->capacity * 2;
		SData* temp = (SData*)realloc(s->a, sizeof(SData) * newcapacity);
		if (temp == NULL)
		{
			perror("realloc failed");
			exit(-1);
		}
		s->a = temp;
		s->capacity = newcapacity;
	}
	s->a[(s->size)] = x;
	s->size++;
}

void StackPop(Stack* s)
{
	assert(s);
	assert(s->size!=0);
	s->size--;
}

SData StackTop(Stack* s)
{
	assert(s);
	assert(s->size != 0);
	return s->a[s->size - 1];
}

void StackPrint(Stack* s)
{
	for (int i = 0; i < s->size; i++)
	{
		printf("%d  ", s->a[i]);
	}
	printf("\n");
}